package _2_0;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	public JTable table;
	
	Employee[] emp = new Employee[100];
	
	private JTextField nameField;
	private JTextField ageField;
	private JTextField salaryField;
	private JTextField bonusField;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenu mnNewMenu_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 582, 426);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnNewMenu = new JMenu("\u83DC\u5355");
		menuBar.add(mnNewMenu);
		
		mntmNewMenuItem = new JMenuItem("New menu item");
		mnNewMenu.add(mntmNewMenuItem);
		
		mntmNewMenuItem_1 = new JMenuItem("New menu item");
		mnNewMenu.add(mntmNewMenuItem_1);
		
		mnNewMenu_1 = new JMenu("\u5458\u5DE5\u7BA1\u7406");
		menuBar.add(mnNewMenu_1);
		
		mntmNewMenuItem_2 = new JMenuItem("\u6DFB\u52A0\u5458\u5DE5");
		mnNewMenu_1.add(mntmNewMenuItem_2);
		mntmNewMenuItem_2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				// 弹一个窗体让用户输入学生数据
				
				AddFrame a = new AddFrame(MainFrame.this);
				a.setVisible(true);
				
				
			}
			
		});
		
		mntmNewMenuItem_3 = new JMenuItem("删除员工");
		mnNewMenu_1.add(mntmNewMenuItem_3);
		mntmNewMenuItem_3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				// 1. 获取到用户当前选择的哪一行
				System.out.println(table.getSelectedRow());
								
				// 2. 从数组中移除
				emp[table.getSelectedRow()] = null;
				
				// 3. 刷新表格
				refreshTable();
				
			}
		});
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		
		// 添加一些数据
		emp[0] = new Driver("张三",28,9000,2000);
		emp[1] = new Driver("李四",30,9000,3000);
		emp[2] = new Driver("王五",27,9000,1000);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		table = new JTable();
		contentPane.add(table);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);
		
		nameField = new JTextField();
		panel.add(nameField);
		nameField.setColumns(10);
		
		ageField = new JTextField();
		panel.add(ageField);
		ageField.setColumns(10);
		
		salaryField = new JTextField();
		panel.add(salaryField);
		salaryField.setColumns(10);
		
		bonusField = new JTextField();
		panel.add(bonusField);
		bonusField.setColumns(10);
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				// 1. 获取填写的信息

				String name = nameField.getText();
				int age = Integer.parseInt(ageField.getText());
				int salary = Integer.parseInt(salaryField.getText());
				int bonus = Integer.parseInt(bonusField.getText());
				
				// 2. 一个员工放入数组
				Employee em = new Driver(name,age ,salary,bonus);
				for(int i=0;i<100;i++)
				{
					if(emp[i]==null)
					{
						emp[i] = em;
						break;
					}
				}
				
				// 3. 刷新一下表格显示
				
				refreshTable();
			}
		});
		panel.add(btnNewButton);
		
		refreshTable();
	}
	
	public void refreshTable()
	{
		String[] cols = {"姓名","年龄","工资","奖金"};
		
		String[][] datas = new String[100][4];
		int tmp = 0;
		for(int i=0;i<100;i++)
		{
			if(emp[i]!=null)
			{

				datas[tmp][0] = ""+emp[i].getName();
				datas[tmp][1] = ""+emp[i].getAge();
				datas[tmp][2] = ""+emp[i].getSalary();
				datas[tmp][3] = ""+((Driver) emp[i]).getBonus();
				tmp++;
			}
		}
		TableModel tm = new DefaultTableModel(datas,cols);
		table.setModel(tm);
			
		table.getColumnModel().getColumn(2).setPreferredWidth(73);
	}

}